package cn.jslancer.webx.client;

import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.concurrent.*;

/**
 * @author jslancer
 * @date 2018/7/24 23:17
 */
public class Client {


    // 并发请求数
    private static int count = 5000;

    // 并发数
    private static int concurrency = 100;

    private static int index = 0;

    private static CountDownLatch countDownLatch = new CountDownLatch(count);

    private static Semaphore semaphore = new Semaphore(concurrency);

    public static void main(String[] args) throws InterruptedException {

        ThreadFactory factory = new DefaultThreadFactory("client");

        ExecutorService executorService = new ThreadPoolExecutor(50, 100, 60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100), factory);

        for (int i = 0; i < count; i++) {

            executorService.submit(() -> {
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                work();
                semaphore.release();
            });
        }
        countDownLatch.await();
        System.out.println(index);
        executorService.shutdown();
    }


    private static void work() {
        index++;
        countDownLatch.countDown();
    }
}
